package com.eric.controller;
import cn.dev33.satoken.util.SaResult;
import com.eric.domain.dto.ApplicantDTO;
import com.eric.domain.vo.ApplicantVO;
import com.eric.service.ApplicantService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *  控制层。
 *
 * @author eric.deng
 * @since 2025-03-05
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/applicant")
@Validated
public class ApplicantController {

    private final ApplicantService applicantService;
    /**
     * 添加。
     *
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @Transactional
    public SaResult save(@Validated @RequestBody ApplicantVO applicantVO) {

        return applicantService.saveOne(applicantVO);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @Transactional
    public SaResult remove(@PathVariable Long id) {
        return SaResult.data(applicantService.removeById(id));
    }

    /**
     * 根据主键更新。
     *
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @Transactional
    public SaResult update(@RequestBody ApplicantDTO applicantDTO) {
        return applicantService.updateByIdService(applicantDTO);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public SaResult getListByPage(Integer cur,Integer pageSize) {

        return applicantService.getListByPage(cur,pageSize);
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public SaResult getInfo(@PathVariable Long id) {
        return SaResult.data(applicantService.getById(id));
    }


    @GetMapping("/allCount")
    public SaResult getAllCounts(){
        long count = applicantService.count();
        return SaResult.data(count);
    }

    @GetMapping("/getByName")
    public SaResult getSupplierListByName(String name,Integer cur,Integer pageSize){
        return applicantService.getApplicantListByNameService(name,cur,pageSize);
    }

    @GetMapping("/getByNameCount")
    public SaResult getSupplierListByNameCount(String name){
        return applicantService.getApplicantListByNameCountService(name);
    }



}
